package com.hm.controller;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.hm.mapper.DeptMapper;
import com.hm.mapper.EmployeeMapper;
import com.hm.model.Employee;
import com.hm.service.EmployeeService;
import com.hm.service.RedisService;

import redis.clients.jedis.Jedis;
/**
 * 所有电话号码8结尾的人以及所在部门的消息
添加一个员工要求所有的字段都要有（部门id用下拉列表表现）
	- 张慧美 56 18889997778  1500  3000 3 
	<select>
		<option value="${dtpt.id}">${dept.deptName}</option>
	</select>
把所有在行政部的人的工资都改成5000
把所有在开发部的人的工资都改成30000
将所有20岁以下的员工开除
 *
 */
@RestController
@RequestMapping("employee")
@SessionAttributes("phones")
public class EmployeeController {

	@Resource
	private Jedis jedis;
	@Resource
	private RedisService redisService;
	
	//@GetMapping("hehe")
	public @ResponseBody Object hehe() throws ClassNotFoundException {
		/*Employee employee = new Employee();
		employee.setname("张春华");
		employee.setuAge(19);
		
		
		try (ByteArrayOutputStream out = new ByteArrayOutputStream();
				ObjectOutputStream oout = new ObjectOutputStream(out);){
			oout.writeObject(employee);
			jedis.set("emp".getBytes(), out.toByteArray());
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		
		/*byte[] bs = jedis.get("emp".getBytes());
		
		try (ObjectInputStream ois= new ObjectInputStream(new ByteArrayInputStream(bs));
				){
			Object readObject = ois.readObject();
			return readObject;
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		return redisService.getEmployee();
	}
	
	
	
	
	
	
	
	
	
	@Resource
	private EmployeeService employeeService;
	
	@Resource
	private EmployeeMapper employeeMapper;
	
	@Resource
	private DeptMapper deptMapper;
	
	
	@GetMapping("all")
	public Object findAll(){
		try {
			return employeeService.findAll();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@GetMapping("insert")
	public ModelAndView insert(Integer id, String name, Integer phone, Integer age, Double uSalary, Double jiangjin) {
		employeeMapper.insert(id, name, phone, age, uSalary, jiangjin);
	 return new ModelAndView("redirect:findAllTwo");
	}
	
	@GetMapping("findAllTwo")
	public  ModelAndView findAllTwo() {
		List<Employee> employees = employeeMapper.findAllTwo();
		return new ModelAndView("employee/employees","employees",employees);
	}
	
	@GetMapping("findByDeptId")
	public Object findByDeptId() {
		return employeeMapper.findByDeptId();
	}
	
	@GetMapping("dept")
	public Object findDept(Integer deptId) {
		return deptMapper.findById(deptId);
	}
	
	@GetMapping("delete")
	public ModelAndView delete(Integer id) {
		employeeMapper.delete(id);
		return new ModelAndView("redirect:findAllTwo"); 
	}
	@GetMapping("findById")
	public ModelAndView findById(Map<String, Object> map,String Key) {
		Employee phones = employeeMapper.findById(Key);
		map.put("phones", phones);
		return new ModelAndView("employee/employees","phones",phones); 
	}
	
}
